using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;

public abstract class C_敌人AI_Base<TStateName, TState> : MonoBehaviour, IBeAttack, I定身术 where TStateName : Enum where TState : IState
{
	public TStateName 当前状态_仅显示;

	public string _EnemyName;

	[Header("用于标记敌人刷新，负数则不判断")]
	public int _EnemyInSceneID = -1;

	private string _EnemyStrID = "";

	[HideInInspector]
	public Rigidbody2D _Rb2D;

	[HideInInspector]
	public Vector2 _StartPos;

	[HideInInspector]
	public Animator _Ani;

	[HideInInspector]
	public C_攻击判断 _C_攻击判断;

	[HideInInspector]
	public GameObject _GO_定身术特效;

	[HideInInspector]
	public Transform _Tsf_玩家;

	[HideInInspector]
	public SpriteRenderer _SR_Model;

	[HideInInspector]
	public SpriteRenderer _SR_影子;

	[HideInInspector]
	public Transform _Tsf_移动目标;

	[HideInInspector]
	public bool _Is视野范围内;

	[HideInInspector]
	public bool _Is追击中;

	[HideInInspector]
	public bool _Is死亡;

	[HideInInspector]
	public bool _Is虚幻;

	protected FSM<TStateName, TState> _FSM;

	public Dictionary<string, float> _Dict_Anis_name_time;

	[HideInInspector]
	public Collider2D _Co2D_自身碰撞器;

	public C_Enemy_Data _Data;

	protected Coroutine _Cor_受击变色;

	protected WaitForSeconds wfsTime1 = new WaitForSeconds(0.15f);

	public int _阵营
	{
		get
		{
			return 2;
		}
		set
		{
			Debug.LogError("WTF??????");
		}
	}

	public Vector2 _BeAttack_Pos { get; set; }

	public float _BeAttack_击退力度 { get; set; }

	public bool _Is被定 { get; set; }

	private void Awake()
	{
		if (_EnemyInSceneID >= 0)
		{
			_EnemyStrID = $"敌人-{MonoSingleton<MySceneManager>.Instance.F_GetCurrSceneID()}-{_EnemyInSceneID}";
			if (!MonoSingleton<GameManager>.Instance.F_EnemyCanInit(_EnemyStrID))
			{
				base.gameObject.SetActive(value: false);
			}
		}
		ItemSO_Data_Enemy data = MonoSingleton<MyDataManager>.Instance._Data_Enemy.F_GetVal(_EnemyName);
		_Data = new C_Enemy_Data(data);
		_Co2D_自身碰撞器 = GetComponent<Collider2D>();
		_Rb2D = GetComponent<Rigidbody2D>();
		_StartPos = base.transform.position;
		_Ani = GetComponent<Animator>();
		_C_攻击判断 = base.transform.Find("攻击判断1").GetComponent<C_攻击判断>();
		_SR_Model = base.transform.Find("Model").GetComponent<SpriteRenderer>();
		_SR_影子 = base.transform.Find("影子").GetComponent<SpriteRenderer>();
		_C_攻击判断._阵营 = _阵营;
		_Is被定 = false;
		_Tsf_移动目标 = base.transform.Find("移动目标");
		_GO_定身术特效 = base.transform.Find("定身术特效").gameObject;
		_Tsf_移动目标.parent = null;
		_Dict_Anis_name_time = new Dictionary<string, float>();
		AnimationClip[] animationClips = _Ani.runtimeAnimatorController.animationClips;
		foreach (AnimationClip animationClip in animationClips)
		{
			_Dict_Anis_name_time.TryAdd(animationClip.name, animationClip.length);
		}
		_FSM = new FSM<TStateName, TState>();
		_FSM.OnStateChange += delegate(TStateName a, TStateName b)
		{
			当前状态_仅显示 = b;
		};
	}

	protected virtual void Update()
	{
		if (!_Is死亡 && !_Is虚幻 && !_Is被定)
		{
			_FSM.OnUpdate();
            int y = (int)(Mathf.Abs((gameObject.transform.position.y - 10) * 5));
            GetComponent<SortingGroup>().sortingOrder = y;
        }
    }

	protected virtual void OnTriggerEnter2D(Collider2D collision)
	{
		if (collision.tag == "Player")
		{
			_Is追击中 = true;
			_Tsf_玩家 = collision.transform;
			_Is视野范围内 = true;
		}
	}

	protected virtual void OnTriggerExit2D(Collider2D collision)
	{
		if (collision.tag == "Player")
		{
			_Is视野范围内 = false;
		}
	}

	public virtual void ChangeState(TStateName state)
	{
		if (!_Is死亡)
		{
			_FSM.ChangeState(state);
		}
	}

	public virtual void BeAttack(float atk_生命, float atk_僵直, float atk_击退力度, Vector2 atk_Pos, bool atk_Is远程 = false)
	{
		if (_Cor_受击变色 != null)
		{
			StopCoroutine(_Cor_受击变色);
		}
		_Cor_受击变色 = StartCoroutine(Cor_受击变色());
		_Rb2D.velocity = 1.1f * _BeAttack_击退力度 * new Vector2((base.transform.position.x > _BeAttack_Pos.x) ? 1 : (-1), 0f);
	}

	protected IEnumerator Cor_受击变色()
	{
		_SR_Model.color = new Color(1f, 0.5f, 0.5f);
		yield return wfsTime1;
		_SR_Model.color = new Color(1f, 1f, 1f);
		_Cor_受击变色 = null;
	}

	public virtual void F_死亡_给玩家经验()
	{
		if (_EnemyInSceneID >= 0)
		{
			MonoSingleton<GameManager>.Instance.F_EnemyDie(_EnemyStrID);
		}
		MonoSingleton<GameManager>.Instance.F_获得经验(_Data._击杀经验值);
	}

	public virtual void F_定身(float 时间)
	{
		_Is被定 = true;
		_Rb2D.velocity = Vector2.zero;
		_Ani.speed = 0f;
		_C_攻击判断.gameObject.SetActive(value: false);
		_GO_定身术特效.SetActive(value: true);
		_GO_定身术特效.transform.localScale = base.transform.localScale;
		Invoke("F_定身结束", 时间);
	}

	public virtual void F_定身结束()
	{
		_Ani.speed = 1f;
		_Is被定 = false;
		_GO_定身术特效.SetActive(value: false);
	}
}
